//
//  DXYMovieViewController.m
//  Piece
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "DXYMovieViewController.h"
#import "DYJMovieTableViewCell.h"
#import "ZHNetworkHandler.h"
#import "DYJMovieListModel.h"
#import "XZHSearchViewController.h"
#import <MJRefresh.h>
#import "XZHMovieDetailViewController.h"
#import "FeHourGlass.h"
#import "ZHArchiverTools.h"
#import "QYXDailyTableViewCell.h"

@interface DXYMovieViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *movieListTableView;
@property (nonatomic, retain) NSMutableArray *movieListDataArray;
@property (nonatomic, copy) NSString *pageNumber;
@property (nonatomic, strong) DYJMovieListModel *movieListModel;
@property (strong, nonatomic) FeHourGlass *hourGlass;
@end

@implementation DXYMovieViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitle:@"电影"];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"电影" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.76 green:0.79 blue:0.83 alpha:1.00];
    [self createNavigationBarButtonWithPicture:@"plant" Picture:@"play"];
    [self createMovieListTableView];
    [self getMovieData];
}
#pragma mark -- 创建movieListTableView
- (void)createMovieListTableView
{
    _movieListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) style:UITableViewStylePlain];
    _movieListTableView.separatorStyle = 0;
    _movieListTableView.delegate = self;
    _movieListTableView.dataSource = self;
    [self.view addSubview:_movieListTableView];
    
    //注册cell
    [_movieListTableView registerClass:[QYXDailyTableViewCell class] forCellReuseIdentifier:@"movieCell"];
    //初始化数据源数组
    _movieListDataArray = [NSMutableArray array];
    _pageNumber = 0;
    //上拉加载
    self.movieListTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self getMovieData];
    }];
}
- (void)myTask
{
    // Do something usefull in here instead of sleeping ...
    sleep(6);
}

#pragma mark -- 数据解析
- (void)getMovieData
{
    if (_pageNumber == 0) {
        _hourGlass = [[FeHourGlass alloc] initWithView:self.movieListTableView];
        [self.view addSubview:_hourGlass];
        [_hourGlass showWhileExecutingBlock:^{
            [self myTask];
        } completion:^{
            
            
        }];
    }
    NSString *urlStr = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/channel/movie/more/%@?platform=ios&uuid=5DA53470-1598-42FF-AB78-E2812C9D28AE&user_id=&version=v4.2.1", _pageNumber];
    NSLog(@"*******%@", urlStr);
    [ZHNetworkHandler getDataByURLString:urlStr WithBodyString:nil WithHeaderDic:nil WithResponse:0 WithSuccess:^(id result) {
        NSDictionary *dictionary = (NSDictionary *)result;
        [ZHArchiverTools archiverObject:dictionary ByKey:@"movieCache" WithPath:@"movie.plist"];
        NSArray *dataArray = [dictionary valueForKey:@"data"];
        for (NSDictionary *dic in dataArray) {
            DYJMovieListModel *movieListModel = [[DYJMovieListModel alloc] initWithDic:dic];
            [_movieListDataArray addObject:movieListModel];
        }
        [_movieListTableView reloadData];
        _movieListModel = [_movieListDataArray lastObject];
        _pageNumber = _movieListModel.id; 
        [_movieListTableView.mj_footer endRefreshing];
        [_hourGlass removeFromSuperview];
    } WithFailure:^(NSError *error) {
        NSDictionary *dictionary = [ZHArchiverTools unarchiverObjectByKey:@"movieCache" WithPath:@"movie.plist"];
        if (dictionary == nil) {
            
        }
        else
        {
            NSArray *dataArray = [dictionary valueForKey:@"data"];
            for (NSDictionary *dic in dataArray) {
                DYJMovieListModel *movieListModel = [[DYJMovieListModel alloc] initWithDic:dic];
                [_movieListDataArray addObject:movieListModel];
            }
            [_movieListTableView reloadData];
            _movieListModel = [_movieListDataArray lastObject];
            _pageNumber = _movieListModel.id;
            [_movieListTableView.mj_footer endRefreshing];
            [_hourGlass removeFromSuperview];

        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _movieListDataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    return ScreenHeight / 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QYXDailyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell"];
    cell.model = [_movieListDataArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYJMovieListModel *model = [_movieListDataArray objectAtIndex:indexPath.row];
    XZHMovieDetailViewController *detailVC = [[XZHMovieDetailViewController alloc] init];
    detailVC.movie_id = model.item_id;
    detailVC.movie_score = model.score;
    detailVC.movie_title = model.title;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(QYXDailyTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell cellOffset];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray<QYXDailyTableViewCell *> *array = [self.movieListTableView visibleCells];
    [array enumerateObjectsUsingBlock:^(QYXDailyTableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj cellOffset];
    }];
    
    if (self.movieListTableView.contentOffset.y < - 120) {
        XZHSearchViewController *svc = [[XZHSearchViewController alloc] init];
        [self.navigationController pushViewController:svc animated:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    //[self.view bringSubviewToFront:self.playView];
    [_hourGlass showWhileExecutingBlock:^{
        //[self myTask];
    } completion:^{
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
